<?php
namespace haibao\cms\business\activity;

use  \haibao\cms\data\activity\ActivityKeyword  as ActivityKeywordData;
use \haibao\comment\business\BusinessException;
use haibao\cms\model\filter\activity\ActivityKeyword as ActivityKeywordFilter;
use haibao\frame\data\query\Condition;
use \haibao\cms\business\activity\activity as ActivityBusiness;

class ActivityKeyword extends \haibao\cms\business\Base{
    public static function getActivityKeywordList($filter,$isGetOne = TRUE){
        $activityData = new ActivityKeywordData;
        if($isGetOne){
            return $activityData->getOne($filter);
        }else{
            return $activityData->getAll($filter);
        }
    }
    
    public static function getActivityKeywordById($Id){
        if(!$Id){
            return false;
        }
        $activityData = new ActivityKeywordData;
        return $activityData->getOneById($Id);
    }
    
    public static function updateActivityKeywordById($editActivityKeywordModel){
        \haibao\cms\business\Base::checkOperation(self::FUNC_CODE_ACTIVITY_KEYWORD_EDIT);
        self::checkModelData($editActivityKeywordModel,true);
        $activityData = new ActivityKeywordData;
        $activityData->updateById($editActivityKeywordModel->Id, $editActivityKeywordModel);
        ActivityBusiness::delActivityCache($editActivityKeywordModel->ActivityId);
        \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_ACTIVITY_KEYWORD_EDIT, $editActivityKeywordModel->toArray(false));
    }
    
    public static function addActivityKeyword($addActivityKeywordModel){
        \haibao\cms\business\Base::checkOperation(self::FUNC_CODE_ACTIVITY_KEYWORD_ADD);
        self::checkModelData($addActivityKeywordModel);
        $activityData = new ActivityKeywordData();
        $activityData->add($addActivityKeywordModel);
        ActivityBusiness::delActivityCache($addActivityKeywordModel->ActivityId);
        \haibao\cms\business\Base::saveOperateHistory(self::FUNC_CODE_ACTIVITY_KEYWORD_ADD, $addActivityKeywordModel->toArray(false));
    }
    private static function checkModelData($addActivityKeywordModel,$isUpdate = FALSE){
        if($isUpdate){
            if(!$addActivityKeywordModel->Id){
                throw new BusinessException('Id不可以为空！');
            }
        }
        if(!$addActivityKeywordModel->Name){
            throw new BusinessException('Name不可以为空！');
        }
        if(!$addActivityKeywordModel->ShowTime){
            throw new BusinessException('ShowTime不可以为空！');
        }
    }
    
    public static function delActivityKeywordById($id, $arrSet){
        \haibao\cms\business\Base::checkOperation(self::FUNC_CODE_ACTIVITY_KEYWORD_DEL);
        if(!$id){
            throw new BusinessException('Id不可以为空！');
        }
        
        $activityData = new ActivityKeywordData();
        
        $data = $activityData->getOneById($id);
        $activityData->updateById($id, $arrSet);
        
        ActivityBusiness::delActivityCache($data->ActivityId);
        
        self::saveOperateHistory(self::FUNC_CODE_ACTIVITY_KEYWORD_DEL, array($id,$arrSet));
    }
    
    public static function isHaveSameKeywordName($id,$name,$activityId,$showTime){
        $activityKeywordFilter = new ActivityKeywordFilter();
        $activityKeywordFilter ->select(array(
            ActivityKeywordFilter::CONDITION_FIELD_NAME_ID,
            ActivityKeywordFilter::CONDITION_FIELD_NAME_ACTIVITYID,
            ActivityKeywordFilter::CONDITION_FIELD_NAME_NAME,
            ActivityKeywordFilter::CONDITION_FIELD_NAME_CREATETIME,
            ActivityKeywordFilter::CONDITION_FIELD_NAME_STATUS,
            ActivityKeywordFilter::CONDITION_FIELD_NAME_SHOWTIME
        ));
        if($id){
            $activityKeywordFilter->where(ActivityKeywordFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL,$id);
        }
        $activityKeywordFilter->where(ActivityKeywordFilter::CONDITION_FIELD_NAME_ACTIVITYID, Condition::CONDITION_EQUAL, $activityId);
        $activityKeywordFilter->where(ActivityKeywordFilter::CONDITION_FIELD_NAME_NAME, Condition::CONDITION_EQUAL, $name);
        $activityKeywordFilter->where(ActivityKeywordFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\activity\ActivityKeyword::STATUS_ENABLE);
        $activityKeywordFilter->where(ActivityKeywordFilter::CONDITION_FIELD_NAME_SHOWTIME, Condition::CONDITION_EQUAL, $showTime);
        $activityData = new ActivityKeywordData();
        $activityKeywordInfo = self::getActivityKeywordList($activityKeywordFilter,true);
        if($activityKeywordInfo){
            return $activityKeywordInfo->Id;
        }else{
            return false; 
        }
    }
}